//
//  MainSubViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/10/17.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "MainSubViewModel.h"

#import "CommonItemViewModel.h"
#import "CommonNewsItemViewModel.h"

@interface MainSubViewModel ()

@end

@implementation MainSubViewModel

#pragma mark - Ovrride
- (id)init
{
    self = [super init];
    
    if (self)
    {
        [self initialize];
    }
    
    return self;
}

- (void)initialize
{
    @weakify(self);
    
    DataManager* dm = [DataManager sharedDataManager];
    self.provinceListData = dm.locationPLData;
    
    // 通知
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:Notify_FirstPageReload object:nil] subscribeNext:^(id x)
    {
        @strongify(self);
        
        NSNotification* notification = x;
        
        self.provinceListData = notification.object;
    }];
    
    // 地区
    [[RACObserve(self, provinceListData) takeUntil:[self rac_willDeallocSignal]] subscribeNext:^(id x)
    {
        @strongify(self);
        
        [self.reqDataCmd execute:nil];
    }];
    
    // 按钮
    self.jglbCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self)
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                    
                    return nil;
                }];
    }];
    
    // 今日焦点
    TodayFocusTableViewModel* todayFocusTVM = [[TodayFocusTableViewModel alloc] initWithParams:@{@"Name":@"TodayFocus"}];
    todayFocusTVM.perPage = List_PageSize;
    todayFocusTVM.lastPage = 1000;
    todayFocusTVM.cellHeight = 212;
    todayFocusTVM.shouldRequestRemoteDataOnViewDidLoad = NO;
    todayFocusTVM.tag = 10;
    self.todayFocusTVM = todayFocusTVM;
    
    self.changeTFCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self)
        
        [self.todayFocusTVM.requestDataCmd execute:@{@"LoadIndex":@(self.todayFocusChangeIndex)}];
        
        self.todayFocusChangeIndex++;
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
        {
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
            
            return nil;
        }];
    }];
    
    // 环保新闻
    NewsTableViewModel* newsTVM = [[NewsTableViewModel alloc] initWithParams:@{@"Name":@"News"}];
    newsTVM.perPage = List_PageSize;
    newsTVM.lastPage = 1000;
    newsTVM.cellHeight = 94;
    newsTVM.shouldRequestRemoteDataOnViewDidLoad = NO;
    newsTVM.tag = 11;
    self.newsTVM = newsTVM;
    
    self.changeNewsCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
                        {
                            @strongify(self)
                            
                            [self.newsTVM.requestDataCmd execute:@{@"LoadIndex":@(self.newsChangeIndex)}];
                            
                            self.newsChangeIndex++;
                            
                            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                                    {
                                        [subscriber sendNext:nil];
                                        [subscriber sendCompleted];
                                        
                                        return nil;
                                    }];
                        }];
    
    self.reqDataCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        
        @weakify(self)
        
        return [[self requestCarousesDataSignal] then:^RACSignal *
        {
            @strongify(self);
            
            return [self requestMainIndexsDataSignal];
        }];
        
        //        return [[[self requestCarousesDataSignal]
        //                 then:^RACSignal *{
        //                     @strongify(self)
        //                     return [self requestEPNewsesDataSignal];
        //                 }]
        //                then:^RACSignal *{
        //                    @strongify(self)
        //                    return [self requestEPPublicitiesDataSignal];
        //                }];
    }];
}

- (RACSignal *)requestCarousesDataSignal
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        
        NSString* areaID = HCStrValid(self.provinceListData.areaID);
        
        @weakify(self);
        [[AFNetWorkUtils racPOSTWthURL:url_interface_bannerlist params:interface_bannerlist_params(@"1", areaID)] subscribeNext:^(id x)
         {
             @strongify(self)
             
             BannersData* imgsData = [BannersData modelWithJSON:x];
             self.adImgsData = imgsData;
             
//             NSMutableArray* imgDatas = [NSMutableArray array];
//
//             for (NSInteger index=0; index<3; index++)
//             {
//                 ADImageData* imgData = [[ADImageData alloc] init];
//                 imgData.url = @"http://www.baidu.com";
//                 imgData.imgurl = @"http://img3.redocn.com/tupian/20160108/lvsehuahuizhizhangfanyexiaoguobeijingsucai_5728265.jpg";
//                 [imgDatas addObject:imgData];
//             }
//
//             ADImagesData* imgsData = [[ADImagesData alloc] init];
//             imgsData.datas = imgDatas;
//             self.adImgsData = imgsData;
             
//             self.adImgsData = [ADImagesData modelWithJSON:x];
             
             
             /// 转化数据
             //            @weakify(self);
             //            NSArray *viewModels = [data.datas.rac_sequence map:^HDChargeListItemViewModel *(HDChargeList * chargeList) {
             //                @strongify(self);
             //                HDChargeListItemViewModel *itemViewModel = [[HDChargeListItemViewModel alloc] initWithListItemData:chargeList];
             //
             //                return itemViewModel;
             //            }].array;
             
             [subscriber sendNext:nil];
             [subscriber sendCompleted];
         }   error:^(NSError *error) {
             //错误处理
             
             [subscriber sendNext:nil];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

- (RACSignal *)requestMainIndexsDataSignal
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        
        NSString* areaID = self.provinceListData ? self.provinceListData.areaID : @"";
        
        @weakify(self);
        [[AFNetWorkUtils racPOSTWthURL:url_interface_mainindex params:interface_mainindex_params(areaID)] subscribeNext:^(id x)
         {
             @strongify(self)
             
             self.mainIndexData = [MainIndexData modelWithJSON:x];
             
             /// 转化数据
             @weakify(self);
             NSArray *focusViewModels = [self.mainIndexData.focusDatas.rac_sequence map:^CommonItemViewModel *(ArticleData* articleData) {
                 @strongify(self);
                 CommonItemViewModel *itemViewModel = [[CommonItemViewModel alloc] initWithData:articleData cellHeight:self.todayFocusTVM.cellHeight];
                 
                 return itemViewModel;
             }].array;
             self.todayFocusTVM.dataSource = focusViewModels;
             
             NSArray *newsViewModels = [self.mainIndexData.newsDatas.rac_sequence map:^CommonNewsItemViewModel *(ArticleData* articleData) {
                 @strongify(self);
                 CommonNewsItemViewModel *itemViewModel = [[CommonNewsItemViewModel alloc] initWithData:articleData cellHeight:self.newsTVM.cellHeight];
                 
                 return itemViewModel;
             }].array;
             self.newsTVM.dataSource = newsViewModels;
             
             /// 转化数据
             //            @weakify(self);
             //            NSArray *viewModels = [data.datas.rac_sequence map:^HDChargeListItemViewModel *(HDChargeList * chargeList) {
             //                @strongify(self);
             //                HDChargeListItemViewModel *itemViewModel = [[HDChargeListItemViewModel alloc] initWithListItemData:chargeList];
             //
             //                return itemViewModel;
             //            }].array;
             
             [subscriber sendNext:nil];
             [subscriber sendCompleted];
         }   error:^(NSError *error) {
             //错误处理
             
             [subscriber sendNext:nil];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

@end
